<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="60%"
    :before-close="handleClose"
    v-loading="loading">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <upload-input ref="uploadInput" v-model="form.avatar"></upload-input>
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker
          v-model="form.birthday"
          type="date"
          placeholder="选择日期"
          value-format="yyyyMMdd"
          style="width: 100%">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="出身地" prop="birthPlace">
        <el-input v-model="form.birthPlace"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="summary">
        <el-input type="textarea" v-model="form.summary"></el-input>
      </el-form-item>
      <el-form-item label="详情">
        <tinymce ref="tinymce" v-model="form.details" :height="250" ></tinymce>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" :loading="submitLoading" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import UploadInput from '@/components/UploadInput'
import { createMaster } from '@/api/investigation'
import { getDetails as getMasterDetails, updateMaster } from '@/api/master'
import moment from 'moment'

const form = {
  name: null,
  avatar: null,
  birthday: '',
  birthPlace: null,
  summary: null,
  details: null
}

export default {
  components: {
    Tinymce,
    UploadInput
  },
  data() {
    return {
      title: '添加大师',
      loading: false,
      submitLoading: false,
      dialogVisible: false,
      investigationId: 0,
      masterId: 0,
      form: {}
    }
  },
  created() {
    this.form = Object.assign({}, form)
  },
  methods: {
    show(id, masterId) {
      this.investigationId = id
      this.masterId = masterId
      this.dialogVisible = true
      this.resetForm()
      if (this.masterId) {
        this.getMasterDetails()
      }
    },
    getMasterDetails() {
      this.loading = true
      getMasterDetails(this.masterId).then(res => {
        this.loading = false
        if (res.status === 200) {
          const { name, avatar, birthday, birthPlace, summary, details } = res.data
          this.form.name = name
          this.form.avatar = avatar
          this.form.birthday = moment().format(birthday),
          this.form.birthPlace = birthPlace
          this.form.summary = summary
          this.form.details = details
          this.$refs.tinymce.setContent(details)
        }
      })
    },
    onSubmit() {
      if (this.masterId) {
        this.update()
      } else {
        this.add()
      }
    },
    add() {
      createMaster(this.investigationId, this.form).then(res => {
        if (res.status === 200) {
          this.$message({
            type: 'success',
            message: res.message
          })
          this.dialogVisible = false
          this.$emit('onSave')
        } else {
          this.$message({
            type: 'warning',
            message: res.message
          })
        }
      })
    },
    update() {
      this.submitLoading = true
      updateMaster(this.masterId, this.form).then(res => {
        this.submitLoading = false
        if (res.status === 200) {
          this.$message({
            type: 'success',
            message: res.message
          })
          this.dialogVisible = false
          this.$emit('onSave')
        } else {
          this.$message({
            type: 'warning',
            message: res.message
          })
        }
      })
    },
    handleClose() {
      this.dialogVisible = false
    },
    resetForm() {
      this.form.details = null
      this.$refs.form && this.$refs.form.resetFields();
    }
  }
}
</script>